import Vue from 'vue';
import App from './App';
import Mixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js';
// import uView from "uview-ui"

import { gotopage } from '@/common/gotopage.js';
// 配置公共方法
Vue.prototype.gotoPage = gotopage;

// Vue.use(uView);
// 配置公共方法
import common from '@/common/common.js';
Vue.prototype.$noMultipleClicks = common.noMultipleClicks;
Vue.prototype.$c = common;

Vue.prototype.req = function (param, backpage, backtype) {
    uni.getNetworkType({
        success: function (res) {
            if (res.networkType == 'none') {
                uni.showToast({
                    title: '网络连接异常，请检查网络',
                    icon: 'none'
                });
                return;
            }
        }
    });
    var _self = this,
        url = param.url,
        method = 'POST',
        header = {},
        data = param.data || {},
        Loading = param.Loading || false;

    var token = uni.getStorageSync('token');
    // 我
    // var token = 'bf3dd927642ea5a7a9ad90f1e1d1faab4d61cd33';
    // 客户
    // var token = '801abe6ec37a36b3ff37ded44be13c7bf06b629e';

    //拼接完整请求地址
    var requestUrl = this.siteBaseUrl + url;
    if (method) {
        method = method.toUpperCase(); //小写改为大写
        // console.log(token,11)
        if (method == 'POST') {
            header = {
                'content-type': 'application/x-www-form-urlencoded',
                token: token,
                adid: uni.getStorageSync('_ad_id'),
                clickid: uni.getStorageSync('_click_id')
            };

            // if (uni.getStorageSync('_ad_id')) {
            //   header.adid = uni.getStorageSync('_ad_id')
            // }
            // if (uni.getStorageSync('_click_id')) {
            //   header.clickid = uni.getStorageSync('_click_id')
            // }
        } else {
            header = {
                'content-type': 'application/json'
            };
        }
    } else {
        method = 'GET';
        header = {
            'content-type': 'application/json'
        };
    }
    //用户交互:加载圈
    // console.log(!Loading)
    if (!Loading) {
        uni.showLoading({
            title: '加载中...'
        });
    }
    //网络请求

    uni.request({
        url: requestUrl,
        method: method,
        header: header,
        data: data,
        success: res => {
            // if (requestUrl.indexOf('coupon_ling') != -1) {
            //   uni.showModal({
            //     title: '提示',
            //     content: JSON.stringify(res),
            //     showCancel: true,
            //     success: ({ confirm, cancel }) => {}
            //   })
            // }

            // console.log(res.data)
            if (res.data.status == 1) {
                //返回结果码code判断:1成功,-1错误
                // uni.showToast({
                // 	title: res.data.msg,
                // 	icon: 'none',
                // })
            } else if (res.data.status == 2222) {
            } else if (res.data.status == -9) {
                let pages = getCurrentPages();
                console.log(pages[pages.length - 1].route);
                // uni.setStorageSync('page', pages[pages.length - 1].route)
                setTimeout(() => {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        success() {
                            setTimeout(() => {
                                uni.navigateTo({
                                    url: '/pages/user/bangding'
                                });
                            }, 1500);
                        }
                    });
                }, 100);
            } else if (res.data.status == 0) {
                setTimeout(function () {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    });
                }, 100);
            } else if (res.data.status < 0) {
                var pages = getCurrentPages();
                for (var a = 0; a < pages.length; a++) {
                    console.log(pages[a].route);
                    if (pages[a].route == 'pages/user/index') {
                        uni.setStorageSync('lgurl', pages[a].route);
                        uni.setStorageSync('lgurldata', JSON.stringify(pages[a].options));
                    }
                }
                setTimeout(() => {
                    uni.showToast({
                        title: '请先登录',
                        icon: 'none'
                    });
                }, 100);
                uni.redirectTo({
                    url: '/pages/user/login'
                });
            } else {
                return;
            }
            typeof param.success == 'function' && param.success(res.data);
        },
        fail: e => {
            console.log('网络请求fail:' + JSON.stringify(e));
            typeof param.fail == 'function' && param.fail(e.data);
        },
        complete: () => {
            uni.hideLoading();
            typeof param.complete == 'function' && param.complete();
            return;
        }
    });
};

// 测试
// const baseUrl = 'https://jidimowan.shequtuangou.vip'
// const imgUrl = 'http://ouhechaowan.oss-cn-beijing.aliyuncs.com'
// 正式
// const baseUrl = 'https://ouhechaowan.shequtuangou.vip'
// const imgUrl = 'http://ouhechaowan.oss-cn-beijing.aliyuncs.com'
const baseUrl = 'https://hlswj.cn';
const imgUrl = 'https://hlswj.cn';
Vue.prototype.siteBaseUrl = baseUrl + '/api/';

Vue.prototype.$z_img2 = baseUrl + '/zcq/';
Vue.prototype.$img = url => imgUrl + '/ou_he_chao_wan' + url;
Vue.prototype.$img1 = url => url;

Vue.prototype.$sys = () => uni.getSystemInfoSync();

Vue.config.productionTip = false;
Vue.mixin(Mixin);
App.mpType = 'app';

const app = new Vue({
    ...App
});
app.$mount();
